%
\chapter{Getting Started}

In this chapter, you discover what Linux is and how it relates to its
inspiration, UNIX. You take a guided tour of the facilities provided by a
Linux development system, and write and run your first program. Along the
way, you'll be looking at

\begin{itemize}
	\item UNIX, Linux, and GNU
	\item Programs and programming language for Linux
	\item How to locate development resources
	\item Static and shared libraries
	\item The UNIX philosophy
\end{itemize}


\section{A Introduction to UNIX, Linux, and GNU}

In recent years Linux has become a phenomenon. Hardly a day goes by without
Linux cropping up in the media in some way. We've lost count of the number
of applications that have been made available on Linux and the number of
organizaitons that have adopted it, including some government departments
and city administrations. Major hardware vendors like IBM and Dell now
support Linux, and major software vendors like Oracle support their software
running on Linux. Linux truly has become a viable operating system,
especially in the server market.

Linux owes its success to systems and applications that proceded it: UNIX
and GNU software. This section looks at how Linux came to be and what its
roots are.

\subsection{What Is UNIX?}

The UNIX operation system was originally developed at Bell Laboratories,
once part of the telecommunications giant AT\&T. Designed in the 1970s for
Digital Equipment PDP computers, UNIX has become a very popular multiuser,
multitasking operating system for a wide variety of hardware platforms, from
PC workstations to multiprocessor servers and supercomputers.

\subsubsection{A Brief History of UNIX}

Strictly, UNIX is a trademark administered by The Open Group, and it refers
to a computer operating system that conforms to particular specification.
This specification, known as The Single UNIX Specification, defines the
names of, interfaces to, and behaviors of all mandatory UNIX operating
system functions. The specification is largely a superset of an earlier
series of specifications, the P1003, or POSIX (Portable Operating System
Interface) specifications, developed by the IEEE (Institute of Electrical
and Electronic Engineers).

Many UNIX-like systems are available commercially, such as IBM's AIX, HP's
HP-UN, and Sun's Solaris. Some have been made available for free, such as
FreeBSD and Linux. Only a few systems currently conform to the Open Group
specification, which allows them to be marketed with the name UNIX.

In the past, compatibility among different UNIX systems has been a real
problem, although POSIX was great help in the respect. These days, by
following a few simple rules it is possible to create applications that will
run on all UNIX and UNIX-like systems. You can find more details on Linux
and UNIX standards in Chapter 18.%TODO

\subsubsection{UNIX Philosophy}

In the following chapters we hope to convey a flavor of Linux (and therefore
UNIX) programming. Although programming in C is in many ways the same
whatever the platform, UNIX and Linux developers have a special view of
program and system development.

The UNIX operating system, and hence Linux, encourages a certain programming
style. Following are a few characteristics shared by typical UNIX programs
and systems:

\begin{itemize}
	\item {\bf{Simplicity}}: Many of the most useful UNIX utilities are very
simple and, as a result, small and easy to understand. KISS ``Keep It Small
and Simple,'' is a good technique to learn. Larger, more complex systems are
guaranteed to contain larger, more complex bugs, and debugging is a chore
that we'd all like to avoid!
	\item {\bf{Focus}}: It's often better to make a program perform one task
well than to throw in every feature along with kitchen sink. A program with
``feature bloat'' can be difficult to use and difficult to maintain.
Programs with a single purpose are easier to improve as better algorithms or
interfaces are developed. In UNIX, small utilities are often combined to
perform more demanding tasks when the need arises, rather than trying to
anticipate a user's needs in one large program.
	\item {\bf{Resuable Components}}: Make the core of your application
as library. Well-documented libraries with simple but flexible programming
interfaces can help others to develop variations or apply the techniques to
new application areas. Examples include the \emph{dbm} database library,
which is a suite of reusable functions rather than a single database
management program.
	\item {\bf{Filters}}: Many UNIX applications can be used as filters.
That is, they transform their input and produce output. As you'll see, UNIX
provides facilities that allow quite complex applications to be developed
from other UNIX programs by combining them in novel ways. Of course, this
kind of reuse is enabled by the development methods that we've previously
mentioned.
	\item {\bf{Open File Formats}}: The more successful and popular UNIX
programs use configuration files and data files that are plain ASCII text or
XML. If either of these is an option for your program development, it's a
good choice. It enable users to use standard tools to change and search for
configuration items and to develop new tools for performing new functions on
the data files. A good example of this is the \emph{ctags} source code
cross-reference system, which records symbol location information as regular
expressions suitable for use by searching programs.
	\item {\bf{Flexibility}}: You can't anticipate exactly how ingeniously
users will use your program. Try to be as flexible as possible in you
programming. Try to avoid arbitrary limits on field sizes or number of
records. If you can, write the program so that it's network-aware and able
to run across a network as well as on a local machine. Never assume you know
everything that the user might want to do.
\end{itemize}

\subsection{What Is Linux?}

As you may already know, Linux is a freely distributed implementation of
UNIX-like kernel, the low-level core of an operating system. Because Linux
takes the UNIX system as its inspiration, Linux and UNIX programs are very
similar. In fact, almost all programs written for UNIX can be compiled and
run on Linux. Also, some commercial applications sold for commercial
versions of UNIX can run unchanged in binary form on Linux system.

Linux was developed by Linus Torvalds at the University of Helsinki, with
the help of UNIX programmers from across the Internet. It began as a hobby
inspired by Andy Tanenbaum's Minix, a small UNIX-like system, but has grown
to become a complete system in its own right. The intention is that the
Linux kernel will not incorporate propritary code but will contain nothing
but freely distributable code.

Versions of Linux are now available for a wide variety of computer systems
using many different types of CPUs, including Pcs based on 32-bit and 64-bit
Intel x86 and compatible processors; workstations and servers using Sun
SPARC, IBM PowerPC, AMD Opteron, and Intel Itanium; and even some handheld
PDAs and Sony's Playstation 2 and 3. If it's got a processor, someone
somewhere is trying to get Linux running on it!

\subsection{The GNU Project and the Free Software Foundation}

Linux owes its existence to the cooperative efforts of a large number of
people. The operating system kernel itself forms only a small part of a
usable development system. Commercial UNIX systems traditionally come
bundled with applications that provide system services and tools. For Linux
system, these additional programs have been written by many different
programmers and have been freely contributed.

The Linux community (together with others) supports the concept of free
software, that is, software that is free from restrictions, subject to the
GNU General Public License (the name GNU stands for the recursive \emph{
GNU's Not Unix}). Although there may be a cost involved in obtaining the
software, it can thereafter be used in any way desired and is usually
distributed in source form.

The Free Software Foundation was set up by Richard Stallman, the author of
GNU Emacs, one of the best-known text editors for UNIX and other systems.
Stallman is a pioneer of the free software concept and started the GNU
project, an attempt to create an operating system and development
environment that would be compatible with UNIX, but not suffer the
restrictions of the proprietary UNIX name and source code. GNU may one day
turn out to to very different from UNIX in the way it handles the hardware
and manages running programs, but it will still support UNIX-style
applications.

The GNU Project has already provided the software community with many
applications that closely mimic those found on UNIX systems. All these
programs, so-called GNU software, are distributed under the terms of the GNU
Gerneral Public License (GPL); you can find a copy of the license at
\url{http://www.gnu.org} . This license embodies the concept of
\emph{copyleft} (a takeoff on ``copyright''). Copyleft is intended to
prevent others from placing restrictions on the use of free software.

A few major examples of software from the GNU Project distributed under the
GPL follow:

\begin{itemize}
	\item GCC: The GNU Compiler Collection, containing the GNU C compiler
	\item G++: A C++ compiler, included as part of GCC
	\item GDB: A source code-level debugger
	\item GNU make: A version of UNIX make
	\item Bison: A parser generator compatible with UNIX yacc
	\item bash: A command shell
	\item GNU Emacs: A text editor and enviroment
\end{itemize}

Many other packages have been developed and released using free software
princiles and the GPL, including spreadsheets, source code control tools,
compilers and interpreters, Internet tools, graphical image manipulation
tools such as Gimp, and two complete object-based environments: GNOME and
KDE. We discuss GNOME and KDE in Chapter 16 and 17. %TODO

There is now so much free software available that with the addition of the
Linux kernel it could be said that the goal of a creating GNU, a free
UNIX-like system, has been achieved with Linux. To recognize the
contribution made by GNU software, many people now refer to Linux system in
general as GNU/Linux.

You can learn more about the free software concent at
\url{http://www.gnu.org} .

\subsection{Linux Distributions}

As we have already mentioned, Linux is actually just a kernel. You can
obtain the sources for the kernel to compile and install it on a machine and
then install many other freely distributed software programs to make a
complete Linux installation. These installations are usually referred to as
\emph{Linux systems}, because they consist of much more than just the
kernel. Most of the utilities come from the GNU Project of the Free Software
Foundations.

As you can probably appreciate, creating a Linux system from just source
code is a major undertaking. Fortunately, many people have put together
read-to-install distributions (often called \emph{flavors}), usually
downloadable or on CD-ROMs or DVDs, that contain not just the kernel but
also many other programming tools and utilities. These often include an
implementation of the X Window System, a graphical environment common on
many UNIX systems. The distributions usually come with a setup program and
additional documentation (normally all on the CD[s]) to help you install
your own Linux system. Some well-known distributions, particularly on the
Intel x86 family of processors, are Red Hat Enterprise Linux and its
community-developed cousin Fedora, Novell SUSE Linux and the free openSUSE
variant, Ubuntu Linux, Slackware, Gentoo, and Debian GNU/Linux. Check out
the DistroWatch site at \url{http://distrowatch.com} for details on many
more Linux distributions.

\section{Programming Linux}

Many people think that programming Linux means using C. It's true that UNIX
was originally written in C and that the majority of UNIX applications are
written in C, but C is not the only option available to Linux programmers,
or UNIX programmers for that matter. In the course of the book, we'll
mention a couple of the alternatives.

\begin{center}
\begin{tabular}{|p{12cm}|}
\hline
In fact, the first version of UNIX was written in PDP 7 assembler language
in 1969. C was conceived by Dennis Ritchie around that time, and in 1973 he
and Ken Thompson rewrote essentially the entire UNIX kernel in C, quite a
feat in the days when system software was written in assembly language.\\
\hline
\end{tabular}
\end{center}

A vast range of programming languages are available for Linux system, and
many of them are free and available on CD-ROM collections or from FTP
archive site on the Internet. Here's a partial list of programming languages
available to the Linux programmer:

\begin{center}
\begin{tabular}{| p{3.5cm} | p{3.5cm} | p{3.5cm} |}
\hline
Ada    & C           & C++          \\ \hline
Eiffel & Forth       & Fortran      \\ \hline
Icon   & Java        & JavaScript   \\ \hline
Lisp   & Modula 2    & Modula 3     \\ \hline
Oberon & Objective C & Pascal       \\ \hline
Perl   & PostScript  & Prolog       \\ \hline
Python & Ruby        & Smalltalk    \\ \hline
PHP    & Tcl/Tk      & Bourne Shell \\
\hline
\end{tabular}
\end{center}

We show how you can use a Linux shell (bash) to develop small- to
medium-sized applications in Chapter 2. For the rest of the book, we mainly
concentrate on C. We direct our attention mostly toward exploring the Linux
programming interfaces from the perspective of the C programmer, and we
assume knowledge on the C programming language.

\subsection{Linux Programs}

Linux applications are represented by two special type of files:
\emph{executables} and \emph{scripts}. Executables files are programs that
can be run directly by the computer; they correspond to Windows \emph{.exe}
files. Scripts are collections of instructions for another program, an
interpreter, to follow. These correspond to Windows \emph{.bat} or
\emph{.cmd} files, or interpreted BASIC programs.

Linux doesn't require executables or scripts to have a specific filename or
any extension whatsoever. File system attributes, which we discuss in
Chapter 2, are used to indicate that a file is a program that may be run. In
Linux, you can replace scripts with compiled programs (and vice versa)
without affecting other programs or the people who call them. In fact, at
the user level, there is essentially no difference between the two.

When you log in to a Linux system, you interact with a shell program (often
\emph{bash}) that runs programs in the same way that the Windows command
prompt does. It finds the programs you ask for by name by searching for a
file with the same name in a given set of directories. The directories to
search are stored in a shell variable, \emph{PATH}, in much the same way as
with Windows. The search path (to which you can add) is configured by your
system administrator and will usually contain some standard places where
system programs are stored. These include:

\begin{itemize}
	\item \emph{/bin}: Binaries, programs used in booting the system
	\item \emph{/usr/bin}: User binaries, standard programs available to
users
	\item \emph{/usr/local/bin}: Local binaries, programs specific to an
installation
\end{itemize}

An administrator's login, such as \emph{root}, may use a \emph{PATH}
variable that includes directories where system administration programs are
kept, such as \emph{/sbin} and \emph{/usr/sbin}.

Optional operating system components and third-party applications my be
installed in subdirectories of \emph{/opt}, and installation programs might
add to your \emph{PATH} variable by way of user install scripts.


\begin{center}
\begin{tabular}{|p{12cm}|}
\hline
It's not a good idea to delete directories from PATH unless you are sure
that you understand what will result if you do.\\
\hline
\end{tabular}
\end{center}

Note that Linux, like UNIX, uses the colon (:) character to separate entries
in the PATH variable, rather than the semicolon (;) that MS-DOS and Windows
use. (UNIX chose : first, so ask Microsoft why Windows is different, not why
UNIX is different!) Here's a sample PATH variable:

\begin{lstlisting}[language=bash,frame=none,backgroundcolor=\color{white}]
/usr/local/bin:/bin:/usr/bin:.:/home/neil/bin:/usr/X11R6/bin
\end{lstlisting}

Here the PATH variable contains entries for the standard program locations,
the current directory (.), a user's home directory, and X Window System.

\begin{center}
\begin{tabular}{|p{12cm}|}
\hline
Remember, Linux uses a forward slash (/) to separate directory names in a
filename rather than the backslash (\textbackslash) of Windows. Again, UNIX
got there first.\\
\hline
\end{tabular}
\end{center}

\subsection{Text Editors}

To write and enter the code examples in the book, you'll need to use an
editor. There are many to choose from on a typical Linux system. The vi
editor is popular with many users.

Both of the authors like Emacs, so we suggest you take the time to learn
some of the features of this powerful editor. Almost all Linux distributions
have Emacs as an optional package you can install, or you can get it from
the GNU website at \url{http://www.gnu.org} or a version for graphical
environments at XEmacs site at \url{http://www.xemacs.org} .

To learn more about Emacs, you can use its online tutorial. To do this,
start the editor by running the emacs command, and then type Ctrl+H followed
by t for the tutorial. Emacs also has its entire manual available. When in
Emacs, type Ctrl+H and then i for information. Some versions of Emacs may
have menu that you can use to access the manual and tutorial.

\subsection{The C Compiler}

On POSIX-compliant systems, the C compiler is called c89. Historically, the
C compiler was simply called cc. over the years, different vendors have sold
UNIX-like systems with C compilers with different facilities and options,
but often still called cc.

When the POSIX standard was prepared, it was impossible to define a standard
a standard cc command with which all these vendors would be compatible.
Instead, the committee decided to create a new standard command for the C
compiler, c89. When this command is present, it will always take the same
options, independent of the machine.

On Linux systems that do try to implement the standards, you might find that
any or all of the comands c89, cc and gcc refer to the system C compiler,
usually the GNU C compiler, or gcc. On UNIX systems, the C compiler is
almost always called cc.

In this book, we use gcc because it's provided with Linux distributions and
because it supports the ANSI standard syntax for C. If you ever find
yourself using a UNIX system without gcc, we recommend that you obtain and
install it. You can find it at \url{http://www.gnu.org} . Wherever we use
gcc in the book, simply substitude the relevant command on your system.

% TODO
\subsubsection*{Your First Linux C Program}
In this example you start developing for Linux using C by writing, compiling
, and running your first Linux program. It might as well be that most famous
of all starting points, Hello World.

Here's the source code for the file hello.c:

\lstinputlisting{source/01/hello.c}

%
